//
//  RequestClient.swift
//  ali_vod_video
//
//  Created by cc的mac on 2021/6/9.
//

import Foundation
import Alamofire
import SwiftyJSON


class AlamofireManager {
    
    private static var session: Session!
    
    var callbackjson:((JSON)->Void)?
    
    class func shareInstance() -> Session {
        
        //不应该每次调用此方法都要初始化，应当检测是否初始化过，如果已经初始化就不需要进行初始化
        if session == nil {
            
            let configuration = URLSessionConfiguration.default
            if session == nil {
                session = Alamofire.Session(configuration:configuration)
            }
        }
        return session
    }
}

class RequestJsonData: NSObject {
    
    class func getJSON(url: String, params: [String : String], completed: ((JSON) -> Void)?, failed: ((String?) -> Void)?) {

        
        //打印url链接
        Debug.DebugUrlLinkString(url: url, params: params)
        Debug.DebugPRINT(obj: url)
        Debug.DebugPRINT(obj: params)
        
        AlamofireManager.shareInstance().request(url, method: .post, parameters: params).responseString {(response) in
            if let dataString = response.value {
                do {
                    let json = try JSON(data: response.data!)
                    Debug.DebugPRINT(obj: json)
                    if json["success"].boolValue == true {
                        completed?(json)
                    } else {
                        failed?(json["msg"].stringValue)
                        
                    }
                } catch(let error) {
                    failed?(error.localizedDescription)
                }
            } else {
                if let err: String =  response.error?.localizedDescription {
                    #if DEBUG
                    failed?("请求失败: \(err)")
                    #else
                    if err.contains("java") {
                        failed?("后台数据请求失败")
                    }
                    #endif
                } else {
                    failed?("请求失败: 未知错误")
                }
            }
        }
    }
    
    class func getJson(url: String, params: [String: String], completed: ((JSON) -> Void)?, failedcallback: ((String?) -> Void)?) {
        AlamofireManager.shareInstance().request(url, method: .post, parameters: params).responseString {(response) in
            if let dataString = response.value {
                do {
                    Debug.DebugPRINT(obj: dataString)
                    let json = try JSON(data: response.data!)
                    
                } catch(let error) {
                    failedcallback?(error.localizedDescription)
                }
            } else {
                if let err:String =  response.error?.localizedDescription {
                    #if DEBUG
                        failedcallback?("请求失败: \(err)")
                    #else
                        if err.contains("java") {
                            failedcallback?("后台数据请求失败")
                        }
                    #endif
                } else {
                    failedcallback?("请求失败: 未知错误")
                }
            }
        }
    }
    
}
